package com.v8dn.app.util.json;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @title Json工具类
 * @author lhf
 * @date 2012-11-2 下午10:08:52
 * @description
 */
public class JsonUtil{

	private static Log log=LogFactory.getLog(JsonUtil.class);

	private static final ObjectMapper MAPPER=new ObjectMapper();
	private static final JsonFactory JSONFACTORY=new JsonFactory();

	static{
		log.debug("启动json工具类!");
		MAPPER.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES,false);
	}

	/**
	 * 私用构造主法.因为此类是工具类.
	 */
	public JsonUtil(){
		super();
	}

	/**
	 * @title 转换Java Bean 为 json
	 * @param o
	 *            对象
	 * @return
	 */
	public static String beanToJson(Object o){
		StringWriter sw=new StringWriter(300);
		JsonGenerator jsonGenerator=null;
		try{
			jsonGenerator=JSONFACTORY.createJsonGenerator(sw);
			MAPPER.writeValue(jsonGenerator,o);
			return sw.toString();
		}catch(Exception e){
			return null;
		}finally{
			if(jsonGenerator!=null){
				try{
					jsonGenerator.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @title json 转 javabean
	 * @param json
	 *            json字符串
	 * @param clazz
	 *            对象
	 * @return
	 */
	public static Object jsonToBean(String json,Class clazz){
		try{
			return MAPPER.readValue(json,clazz);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @title 转换Java Bean 为 HashMap
	 * @param o
	 *            对象
	 * @return
	 */
	public static Map<String,Object> beanToMap(Object o){
		try{
			return MAPPER.readValue(beanToJson(o),HashMap.class);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @title 转换Json String 为 HashMap
	 * @param json
	 *            json字符串
	 * @param collToString
	 * @return
	 */
	public static Map<String,Object> jsonToMap(String json,boolean collToString){
		try{
			Map<String,Object> map=MAPPER.readValue(json,HashMap.class);
			if(collToString){
				for(Map.Entry<String,Object> entry:map.entrySet()){
					if(entry.getValue() instanceof Collection||entry.getValue() instanceof Map){
						entry.setValue(beanToJson(entry.getValue()));
					}
				}
			}
			return map;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @title List 转换成json
	 * @param list
	 * @return
	 */
	public static String listToJson(List<Map<String,String>> list){
		JsonGenerator jsonGenerator=null;
		StringWriter sw=new StringWriter();
		try{
			jsonGenerator=JSONFACTORY.createJsonGenerator(sw);
			new ObjectMapper().writeValue(jsonGenerator,list);
			jsonGenerator.flush();
			return sw.toString();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			if(jsonGenerator!=null){
				try{
					jsonGenerator.flush();
					jsonGenerator.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @title json 转List
	 * @param json
	 *            json字符串
	 * @return
	 */
	public static List<Map<String,String>> jsonToList(String json){
		try{
			if(json!=null&&!"".equals(json.trim())){
				JsonParser jsonParse=JSONFACTORY.createJsonParser(new StringReader(json));
				ArrayList<Map<String,String>> arrayList=(ArrayList<Map<String,String>>)new ObjectMapper().readValue(jsonParse,ArrayList.class);
				return arrayList;
			}else{
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args){
	}

}